import { IUserInfo } from '@/api/types/common'
import { getItem, setItem } from '@/utils/storage'
import { defineStore } from 'pinia'
import { USER } from '@/utils/constant'

export interface IUserInfos extends IUserInfo {
  token: string
}

export declare type IUerInfoPayload = IUserInfos | null

export interface MainState {
  // counter: number,
  // name: string,
  isCollapse: boolean,
  user: IUerInfoPayload
}

// main is the name of the store. It is unique across your application
// and will appear in devtools
export const useMainStore = defineStore('main', {
  // a function that returns a fresh state
  state: (): MainState => ({
    // counter: 0,
    // name: 'Eduardo',
    isCollapse: false,
    user: getItem(USER)
  }),
  // optional getters
  getters: {
    // getters receive the state as first parameter
    // doubleCount: (state) => state.counter * 2,
    // // use getters in other getters
    // doubleCountPlusOne (): number {
    //   return this.doubleCount + 1
    // },
  },
  // optional actions
  actions: {
    // reset () {
    //   // `this` is the store instance
    //   this.counter = 0
    // },
    toggleSidebar () {
      this.isCollapse = !this.isCollapse
    },
    setUser (user: IUerInfoPayload) {
      this.user = user
      setItem(USER, JSON.stringify(user))
    }
  },
})
